package com.H1023.controller;

import com.H1023.pojo.Reader;
import com.H1023.pojo.ReaderLend;
import com.H1023.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class ReaderController {

    @Autowired
    @Qualifier("readerServiceImpl")
    private ReaderService readerService;

    //显示所有读者
    @RequestMapping("/allReader")
    public String allReader(Model model) {
        List<Reader> readerList = readerService.getAllReader();
        model.addAttribute("readerList", readerList);

        return "allReader";
    }

    //跳转至新增读者页面
    @RequestMapping("/toAddReader")
    public String toAddReader() {
        return "addReader";
    }

    //新增读者
    @RequestMapping("/addReader")
    public String addReader(Reader reader) {
        System.out.println("reader==>" + reader);
        int i = readerService.addReader(reader);
        if( i == -1){
            //用户名重复，抛出异常，操作失败
            return "fail";
        } else{
            return "redirect:/admin/allReader";
        }
    }

    //跳转至修改读者页面
    @RequestMapping("/toEditReader/{id}")
    public String toEditReader(@PathVariable int id, Model model) {
        Reader reader = readerService.getReaderById(id);
        model.addAttribute("reader", reader);
        return "editReader";
    }

    //修改读者
    @RequestMapping("/editReader")
    public String editReader(Reader reader) {
        int i = readerService.updateReader(reader);
        if( i == -1){
            //用户名重复，抛出异常，操作失败
            return "fail";
        } else{
            return "redirect:/admin/allReader";
        }
    }

    //查询读者
    @RequestMapping("/queryReaderByName")
    public String queryReaderByName(String readerName, Model model) {
        List<Reader> readerList = readerService.queryReaderByName(readerName);
        if (readerList.size() == 0) {
            model.addAttribute("err", "未查到");
            readerList = readerService.getAllReader();
        }

        model.addAttribute("readerList", readerList);
        return "allReader";
    }

    //跳转至读者借阅记录页面
    @RequestMapping("/toReaderLend/{readerId}")
    public String toReaderLend(@PathVariable int readerId, Model model) {
        List<ReaderLend> readerLendList = readerService.queryLendByReader(readerId);
        model.addAttribute("readerLendList", readerLendList);
        if (readerLendList.size() != 0) {
            model.addAttribute("readerName", readerLendList.get(0).getReaderName());
        } else {
            Reader readerById = readerService.getReaderById(readerId);
            model.addAttribute("readerName", readerById.getReaderName());
        }
        return "readerLend";
    }
}
